﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using X6TransByBali.Common;

namespace TCPClient
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        TouchSocketClient touchSocketClient = new TouchSocketClient();

        RichTextBoxPanel richTextBoxPanel;
        private void Form1_Load(object sender, EventArgs e)
        {
            btnStop.Enabled= false;

            touchSocketClient.DataEvent += TouchSocketClient_DataEvent;
            touchSocketClient.ErrEvent += TouchSocketClient_ErrEvent;
            touchSocketClient.ConnectedEvent += TouchSocketClient_ConnectedEvent;
            richTextBoxPanel = new RichTextBoxPanel(richTextBox1);
        }

        private void TouchSocketClient_ConnectedEvent(bool obj)
        {
            if (obj)
            {
                richTextBoxPanel.LogMessage("已连接");
            }
            else {
                richTextBoxPanel.LogMessage("已断开");
            }
        }

        private void TouchSocketClient_ErrEvent(string obj)
        {
            richTextBoxPanel.LogError(obj);
        }

        private void TouchSocketClient_DataEvent(string obj)
        {
            richTextBoxPanel.LogMessage(obj);
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            try
            {
                touchSocketClient.Start(txtIP.Text.Trim(), int.Parse(txtPort.Text.Trim()));
                btnConnect.Enabled = false;
                btnStop.Enabled = true;
            }catch (Exception ex){
                MessageBox.Show(ex.Message);
            }
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            touchSocketClient.Stop();
            btnConnect.Enabled = true;
            btnStop.Enabled = false;
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            touchSocketClient.SendData(txtData.Text.Trim()+"\r\n");
        }
    }
}
